/**
 * 
 */
package com.glela.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.common.constant.CFlagEnum;
import com.glela.common.constant.CommonConstants;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.SMaterialConfig;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseMaterialConfig;
import com.glela.platform.model.response.ResponseMaterials;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.service.IMaterialConfigService;
import com.glela.platform.service.SmsService;
import com.glela.platform.util.ParameterUtils;

/**
 * @author liujie
 */
@Controller("materialConfigController")
@RequestMapping(value = "/config")
public class MaterialConfigController
{

	private static Logger logger = LoggerFactory.getLogger(MaterialConfigController.class);

	@Resource(name = "materialConfigService")
	private IMaterialConfigService materialConfigService;
	@Resource
	private SmsService smsService;
	
	/**
	 * 查询素材配置
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getMaterials/{type}", method = RequestMethod.GET, consumes = "application/json")
	public ResponseEntity<?> getMaterials(@PathVariable Integer type)
	{
		if (ParameterUtils.validParams(type)) {
			logger.error("缺少必传参数00");
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29903), HttpStatus.OK);
		}
		//查询素材配置开关
		Integer readMaterial = materialConfigService.selectSystemFlag(CommonConstants.READ_MATERIAL);
		//响应数据
		Date time = null;
		List<ResponseMaterialConfig> respList = null;
		if (readMaterial == CommonConstants.STATUS_OPEN) {
			//查询素材配置
			List<SMaterialConfig> list = materialConfigService.selectByType(type);
			if (null != list && !list.isEmpty()) {
				respList = new ArrayList<ResponseMaterialConfig>();
				time = list.get(0).getUpdateTime();
				for (SMaterialConfig sf : list) {
					ResponseMaterialConfig rc = new ResponseMaterialConfig();
					rc.setType(sf.getMaterialKey());
					rc.setUrl(sf.getUrl());
					respList.add(rc);
				}
			}
		}
		ResponseMaterials materials = new ResponseMaterials();
		materials.setTime(time);
		materials.setMaterial(respList);
		materials.setReadMaterial(readMaterial);
		return new ResponseEntity<>(new ResponseResultModel<ResponseMaterials>(materials), HttpStatus.OK);
	}
	
	/**
	 * 查询语言验证码开关状态
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/voiceSms", method = RequestMethod.GET)
	public ResponseEntity<?> voiceSms(HttpServletRequest request){
		try{
			Integer apply_credit_status = smsService.getVoiceVerifyCodeStatus(CFlagEnum.apply_credit.getKey());
			Map<String,Integer> data = new HashMap<String,Integer>();
			data.put(CFlagEnum.apply_credit.getKey(), apply_credit_status);
			return new ResponseEntity<>(new ResponseResultModel<Map<String,Integer>>(data), HttpStatus.OK);
		}catch(Exception e){
			logger.error("查询语言验证码开关状态",e);
		}
		return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
	}
}
